package cn.zc.Scheduled;

import cn.zc.constant.MessageConstant;
import cn.zc.entity.CourceSQL;
import cn.zc.entity.LateTimeCource;
import cn.zc.entity.TimeVo;
import cn.zc.entity.User;
import cn.zc.mapper.AdminMapper;
import cn.zc.mapper.CourceMapper;
import cn.zc.mapper.StudentMapper;
import cn.zc.service.AdminService;
import cn.zc.service.CourceService;
import cn.zc.utils.CourceUtils;
import cn.zc.utils.QiandaoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.DayOfWeek;
import java.util.List;

@EnableScheduling
@Component
public class MyScheduledTask {
    private volatile boolean shouldPerformTask = true;
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private CourceMapper courceMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Scheduled(cron = "0 0 0 * * SUN")
//        @Scheduled(fixedRate = 1000)
        public void setShouldPerformTask(){
            //当前周加1
            courceMapper.addWeek();
//            //清空登录日志
            adminService.clearLoginLog();
            //设置课程的状态是否签到，一个一个人设置
        List<User> userInfo = adminMapper.getUserInfo();
        for (User user : userInfo){
            List<CourceSQL> courceVo = courceMapper.getcourcevoByid(user.getId());
            List<CourceSQL> courceSQLS = CourceUtils.setIsOverlap(courceVo, courceMapper.getWeek());
            //开始一次修改isover
            for (CourceSQL c:courceSQLS){
                courceMapper.upUserIsover(c);
            }
        }
        //删除所有迟到数据
        studentMapper.deleteAllViolation();
    }
//    @Scheduled(fixedRate = 1000*60*60*24)
//    public void setShouldPerformTask(){
//        DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
//        int value = dayOfWeek.getValue();
//        if(value==7){
//            //当前周加1
//            courceMapper.addWeek();
//            //清空登录日志
//            adminService.clearLoginLog();
//            //更新isover
////            courceService.updateIsOver();
//        }
//    }
//    @Scheduled(fixedRate = 1000*5)
//    @Transactional// 每 5 秒钟执行一次
//    public void performTask() {
////        courceService.updateIsOver();
//        if(shouldPerformTask){
//        List<TimeVo> time = adminService.getTime();
//        for (TimeVo timeVo : time) {
//            LocalTime endTime = QiandaoUtils.parseTime(timeVo.getEnd_time());
//            LocalTime endTimePlusOneMinute = endTime.plusMinutes(1);
//            LocalTime currentTime = LocalTime.now();
//            if((currentTime.isAfter(endTime) && currentTime.isBefore(endTimePlusOneMinute))){
//                int period = timeVo.getPeriod();
//                System.out.println("第"+period+"节");
//                //获得当前是周几
//                DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
//                int dayOfWeekValue = dayOfWeek.getValue();
//                System.out.println("周"+dayOfWeekValue);
//                courceService.lateTime(period, dayOfWeekValue);
//                //向违纪表中添加数据
//                List<LateTimeCource> lateTimeCources = courceService.selectlateTime(period, dayOfWeekValue);
//                for (LateTimeCource lateTimeCource : lateTimeCources) {
//                    //默认违纪是迟到
//                    lateTimeCource.setViolation_content(MessageConstant.Late);
//                    lateTimeCource.setDayOfWeek(dayOfWeekValue);
//                    lateTimeCource.setWeek(courceMapper.getWeek());
//                    courceService.addLateTimeCource(lateTimeCource);
//                    System.out.println("成功添加"+lateTimeCource);
//                    shouldPerformTask=false;
//                }
//            }
//        }
////        System.out.println("执行");
//    }
//    }
//    @Scheduled(fixedRate = 1000*60*30)
//    public void resetShouldPerformTask(){
//        if(!shouldPerformTask){
//            shouldPerformTask=true;
//        }
//    }
}
